<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 列表
 *
 * @author      Eden
 * @datetime    2016/12/14 20:16
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Lists extends ADM_Controller
{

    // 错误
    private $error = '';

    // 第页条数
    const PAGE_SIZE = 10;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 列表页面
     */
    public function index()
    {

        $page = $this->input->get('page');
        $page = intval($page) > 0 ? intval($page) : 1;
        $offset = ($page - 1) * self::PAGE_SIZE;

        $this->load->model('admin/goods/Goods_Model', 'goods');
        $res = $this->goods->getGoodsLists($offset, self::PAGE_SIZE);

        $this->load->library('core/Page', null, 'page');
        $this->load->view(parent::TMP_DIR . '/goods/index.html', array(
            'goods' => !empty($res) ? $res['data'] : array(),
            'page' => $this->page->getPage('/goods/lists/index', $res['total'], self::PAGE_SIZE)
        ));
    }

    /**
     * API获取
     */
    public function api()
    {
        $send['state'] = false;
        do {

            $page = $this->input->get('page');
            $page = intval($page) > 0 ? intval($page) : 1;
            $offset = ($page - 1) * self::PAGE_SIZE;

            $this->load->model('admin/goods/Goods_Model', 'goods');
            $res = $this->goods->getGoodsLists($offset, self::PAGE_SIZE);
            $send['state'] = true;
            $send['data'] = [
                'nowPage' => $page,
                'totalPage' => ceil($res['total'] / self::PAGE_SIZE),
                'data' => $res['data']
            ];

        }while(false);
        $this->output->send($send);
    }

    public function add()
    {
        $this->load->view(parent::TMP_DIR . '/goods/add.html');
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $this->load->view(parent::TMP_DIR . '/goods/edit.html');
    }

    /**
     * 获取商品详情
     */
    public function getGoodsInfo()
    {
        $send['state'] = false;
        do {

            $goodsId = $this->input->get('goodsId');
            if (empty($goodsId)) {
                $send['error'] = '非法请求';
                break;
            }

            $this->load->model('admin/goods/Goods_Model', 'goods');
            $res = $this->goods->getGoodsInfo($goodsId);
            if (!$res) {
                $err = $this->goods->getError();
                $send['error'] = !empty($err) ? $err : '商品查询失败';
                break;
            }

            $send['state'] = true;
            $send['data'] = $res;

        } while(false);
        $this->output->send($send);
    }


    /**
     * 保存商品
     */
    public function saveGoods()
    {
        $send['state'] = false;
        do {

            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            $params = $this->input->post();
            if (!$this->checkParams($params)) {
                $send['error'] = $this->error;
                break;
            }

            $this->load->model('admin/goods/Goods_Model', 'goods');
            if ($this->goods->addGoods($params)) {
                $send['state'] = true;
                @statIncr('goodsIncr');
                break;
            }

            $send['error'] = '商品添加失败';
        } while(false);
        $this->output->send($send);
    }

    /**
     * 编辑商品
     */
    public function editGoods()
    {
        $send['state'] = false;
        do {

            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            $params = $this->input->post();
            if (empty($params['goodsId']) || intval($params['goodsId']) <= 0) {
                $send['error'] = '商品不存在';
                break;
            }

            if (!$this->checkParams($params)) {
                $send['error'] = $this->error;
                break;
            }

            $this->load->model('admin/goods/Goods_Model', 'goods');
            if ($this->goods->editGoods($params)) {
                $send['state'] = true;
                break;
            }

            $send['error'] = '商品添加失败';
        } while(false);
        $this->output->send($send);
    }

    /**
     * 删除商品
     */
    public function deleteGoods()
    {
        $send['state'] = false;
        do {
            $goodsId = $this->input->get('goodsId');
            if (empty($goodsId)) {
                $send['error'] = '无此商品';
                break;
            }

            $this->load->model('admin/goods/Goods_Model', 'goods');
            if ($this->goods->changeGoods($goodsId, 'delete')) {
                $send['state'] = true;
            } else {
                $err = $this->goods->getError();
                $send['error'] = !empty($err) ? $err : '删除失败';
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 修改状态
     */
    public function changeStatus()
    {
        $send['state'] = false;
        do {
            $goodsId = $this->input->get('goodsId');
            $status = $this->input->get('option');
            if (empty($goodsId)) {
                $send['error'] = '无此商品';
                break;
            }

            if (!in_array($status, array('up', 'down'))) {
                $send['error'] = '错误的操作类型';
                break;
            }

            $this->load->model('admin/goods/Goods_Model', 'goods');
            $this->goods->setStauts($status);
            if ($this->goods->changeGoods($goodsId, 'status')) {
                $send['state'] = true;
            } else {
                $err = $this->goods->getError();
                $send['error'] = !empty($err) ? $err : '状态修改失败';
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 整理参数
     * @param $params
     * @return bool
     */
    private function checkParams($params)
    {
        if (empty($params['goodsName']) || empty($params['categoryId']) || empty($params['baseId']) || empty($params['rules'])
        || empty($params['thumbLists']) || empty($params['content'])) {
            $this->error = '有必须项没有填写';
            return false;
        }

        if (empty($params['unit'])) {
            $params['unit'] = '个';
        }

        return $params;
    }

    /**
     * 获取配置
     */
    public function getConfigs()
    {
        $this->output->send(array(
            'cat' => $this->getCats(),
            'unit' => $this->getUnit(),
            'bases' => $this->getBases(),
            'stream' => $this->getStream()
        ));
    }

    /**
     * 获取基地名称
     * @return mixed
     */
    private function getBases()
    {
        $this->load->model('admin/common/Common_Model', 'com');
        return $this->com->getAllBaseName('id, name');
    }

    /**
     * 获取单位
     * @return mixed
     */
    private function getUnit()
    {
        $this->load->model('admin/common/Common_Model', 'com');
        return $this->com->getUnits();
    }

    /**
     * 获取分类
     * @return mixed
     */
    private function getCats()
    {
        $this->load->model('admin/common/Common_Model', 'com');
        return $this->com->getAllGoodsCategories();
    }

    /**
     * 获取直播流
     * @return mixed
     */
    private function getStream()
    {
        $this->load->model('admin/stream/Device_Model', 'stream');
        return $this->stream->getAllLists();
    }
}
